package com.wzw.springboot.principle.use.security.infrastructure.model;

import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * 自定义用户登入信息
 *
 * @author Wangzhiwen
 */
public class SecurityUser implements UserDetails, CredentialsContainer {

    /**
     * ID
     */
    @Setter
    @Getter
    private Long id;

    /**
     * 名称
     */
    @Setter
    private String username;

    /**
     * 账号
     */
    @Setter
    @Getter
    private String account;

    /**
     * 密码 AbstractUserDetailsAuthenticationProvider 会检查密码 不一致时会报 BadCredentialsException
     */
    @Setter
    private String password;

    /**
     * 手机号
     */
    @Setter
    @Getter
    private String mobile;

    /**
     * 邮箱
     */
    @Setter
    @Getter
    private String email;

    /**
     * 角色编码集合
     */
    @Setter
    @Getter
    private Set<String> roles;

    /**
     * 权限标识列表
     */
    @Getter
    @Setter
    private Set<String> permissions;

    private List<GrantedAuthority> authorities;

    /**
     * token
     */
    @Setter
    @Getter
    private String uuid;

    /**
     * 登录时间
     */
    @Setter
    @Getter
    private Long loginTime;

    /**
     * 失效时间
     */
    @Setter
    @Getter
    private Long expireTime;

    /**
     * 用户是否启用
     * true 启用  false 禁用
     * 默认设为true 在 AbstractUserDetailsAuthenticationProvider 验证用户的过程中，会去检查；如果为false会报 DisabledException
     */
    private Boolean enabled = true;

    /**
     * 用户账号是否已过期
     * true 代表未过期  false 代表已过期
     * 默认设为true 在 AbstractUserDetailsAuthenticationProvider 验证用户的过程中，会去检查；如果为false会报 AccountExpiredException
     */
    private Boolean accountNonExpired = true;

    /**
     * 用户账号是否已被锁
     * true 代表已锁定  false 代表未锁定
     * 默认设为true 在 AbstractUserDetailsAuthenticationProvider 验证用户的过程中，会去检查；如果为false会报 LockedException
     */
    private Boolean accountNonLocked = true;

    /**
     * 用户账号密码是否有凭证
     * true 有  false 无
     * 默认设为true 在 AbstractUserDetailsAuthenticationProvider 验证用户的过程中，会去检查；如果为false会报 CredentialsExpiredException
     */
    private Boolean credentialsNonExpired = true;

    @Setter
    @Getter
    private String jwtToken;

    public SecurityUser setAuthorities(List<GrantedAuthority> authorities) {
        this.authorities = authorities;
        return this;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public SecurityUser setEnabled(Boolean enabled) {
        this.enabled = enabled;
        return this;
    }

    public SecurityUser setAccountNonExpired(Boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
        return this;
    }

    public SecurityUser setAccountNonLocked(Boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
        return this;
    }

    public SecurityUser setCredentialsNonExpired(Boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
        return this;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    @Override
    public void eraseCredentials() {
        this.password = null;
    }
}
